using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using Camalon.Drawing;

namespace Camalon.Drawing.Design
{
	public class ShadeEditor:UITypeEditor
	{

		public override bool GetPaintValueSupported(ITypeDescriptorContext context) 
		{
			return true;
		}

		public override void PaintValue(PaintValueEventArgs pe) 
		{
			if(pe.Value == null || !( pe.Value is Shade)) 
			{
				base.PaintValue( pe);
				return;
			}

			try
			{
				Shade shade = pe.Value as Shade;
				if( shade == null) return;
				ShapeFill shapeFill = shade.Fill;
				Rectangle b = pe.Bounds;

				if( shapeFill != null)
				{
					GraphicsPath path = new GraphicsPath();
					path.AddRectangle( b );
					Brush brush = null;

					switch( shapeFill.FillType)
					{
						case FillType.Hatched:
						{
							brush = ((HatchFill)shapeFill).GetBrush();
							break;
						}
						case FillType.LinearGradient:
						{
							brush =((LinearGradientFill) shapeFill).GetBrush( b );
							break;
						}
						case FillType.PathGradient:
						{
							brush = ((PathGradientFill)shapeFill).GetBrush( path);
							break;
						}
						case FillType.Solid:
						{
							brush =((SolidFill) shapeFill).GetBrush();
							break;
						}
						case FillType.Texture:
						{
							brush = ((TextureFill)shapeFill).GetBrush();
							break;
						}
					}

					pe.Graphics.FillPath( brush, path );
					path.Dispose();
					brush.Dispose();

				}

				b.Inflate( -2,-2);
				Pen pen = ShapePenEditor.GetPen(shade.Pen, b , 3f);
				if( pen == null) return;

				pen.EndCap = LineCap.Flat;
				pen.StartCap = LineCap.Flat;

				Point p1 = new Point( b.Right, b.Top);
				Point p2 = new Point( b.Right, b.Bottom);
				Point p3 = new Point( b.Left, b.Bottom);
				Point[] pts = new Point[]{p1,p2, p3};

				pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
				pe.Graphics.DrawLines(pen, pts);
				pe.Graphics.SmoothingMode = SmoothingMode.Default;
				pen.Dispose(); pen = null;

			}
			catch( Exception e )
			{
				FileWatcher.WriteLine("ShapeFillEditor::PaintValue failed:"+ e.Message +"\n"+e.StackTrace);
				throw e;
			}

		}

		
	
	}
}
